<template>
    <Card :padding="10" dis-hover>
        <Row>
            <Col>
            <Form :model="formItem" ref="form" :label-width="100" inline style="padding-top:8px;">
                <FormItem label="标准编码" prop="code1"><Input v-model="formItem.code1" placeholder=""
                        style="width: 100px" />
                </FormItem>
                <FormItem label="标准名称" prop="name1"><Input v-model="formItem.name1" placeholder=""
                        style="width: 150px" />
                </FormItem>
                <FormItem label="本地编码" prop="code2"><Input v-model="formItem.code2" placeholder=""
                        style="width: 100px" />
                </FormItem>
                <FormItem label="本地名称" prop="name2"><Input v-model="formItem.name2" placeholder=""
                        style="width: 150px" />
                </FormItem>
                <FormItem label="类别名称" prop="name3"><Input v-model="formItem.name3" placeholder=""
                        style="width: 100px" />
                </FormItem>
            </Form>
            </Col>
            <Col>
            <Space><Button type="primary" icon="ios-search" @click="initList(1, 15)">搜索</Button><Button type="error"
                    @click="clear" icon="md-refresh">重置</Button><Button type="success" @click="add(1, {})"
                    icon="md-add">新增</Button>
                <Upload ref="upload" :action="upload_url" :headers="headers" :on-success="handleSuccess"
                    :on-progress="handleProgress" :data="{ group: '辅助目录关系' }" style="padding-top:9px;">
                    <Button type="info" icon="md-cloud-upload">导入</Button>
                </Upload>
            </Space>
            </Col>
        </Row>
        <Row>
            <Col span="24">
            <Table :paras="{
                columns: table.columns,
                data: table.data,
                loading: table.load,
                pageNo: formItem.pageNo,
                pageSize: formItem.pageSize,
            }">
                <template #Action="{ row }">
                    <Space>
                        <Tooltip content="编辑" placement="top" transfer><Button type="primary" icon="md-create"
                                @click="add(2, row)"></Button></Tooltip>
                        <Poptip transfer confirm title="是否删除?" @on-ok="cancel(row.spid)" @on-cancel="cancel" ok-text="是"
                            cancel-text="否">
                            <Tooltip content="删除" placement="top" transfer><Button type="error"
                                    icon="md-close-circle"></Button></Tooltip>
                        </Poptip>
                    </Space>
                </template>
            </Table>
            <Pager :paras="{ total: table.total, page: formItem.pageNo, size: formItem.pageSize }" @toPage="initList"
                @toSize="initList">
            </Pager>
            </Col>
        </Row>
    </Card>
    <AddOrUpdate ref="modal" @initList="initList(1, 15)"></AddOrUpdate>
</template>
<script setup>
import http from '@/util/http'
import { reactive } from 'vue'
import Table from '@/components/system/Table.vue'
import Pager from '@/components/system/Pager.vue'
import AddOrUpdate from './AddOrUpdate.vue'
let { proxy } = getCurrentInstance()
let form = ref()
let upload = ref()

let modal = ref()
let upload_url = import.meta.env.VITE_BASEURL + "/api/Basic/PolicyEnum/Import"
var token = sessionStorage.getItem("token");
var tenantID = sessionStorage.getItem("tenant");

let headers = {
    Authorization: "Bearer " + token,
    TenantID: tenantID,
};

let formItem = reactive({
    group: '辅助目录关系',
    code1: '',
    code2: '',
    name1: '',
    name2: '',
    code3: '',
    name3: '',
    pageSize: 15,
    pageNo: 1,
})
let table = reactive({
    data: [],
    total: 0,
    columns: [
        {
            title: '标准编码',
            align: 'center',
            key: 'code1',
        },
        {
            title: '标准名称',
            align: 'center',
            key: 'name1',
        }, {
            title: '本地编码',
            align: 'center',
            key: 'code2',
        },
        {
            title: '本地名称',
            align: 'center',
            key: 'name2',
        }, {
            title: '类别代码',
            align: 'center',
            key: 'code3',
        }, {
            title: '类别名称',
            align: 'center',
            key: 'name3',
        },
        {
            title: '添加时间',
            align: 'center',
            key: 'addTime',
        },
        {
            title: '修改时间',
            align: 'center',
            key: 'updTime',
        },
        {
            title: '操作',
            slot: 'Action',
            align: 'center',
        },
    ],
    load: true,
})

const initList = (page, size) => {
    formItem.pageNo = page
    formItem.pageSize = size
    table.load = true

    http.post('/Basic/PolicyEnum/PageList', toRaw(formItem), (response) => {
        table.load = false
        table.data = response.data.data.list
        table.total = response.data.data.total
    })
}

initList(1, 15)

const clear = () => {
    form.value.resetFields()
}

const add = (type, data) => {
    modal.value.show(type, data)
}
const cancel = (enumID) => {
    var enumIDs = []
    enumIDs[0] = enumID
    http.get('/Basic/PolicyEnum/Del?spIDs=' + enumIDs, {}, (response) => {
        if (response.data.flag) {
            initList(1, 15)
            proxy.$Message.success(response.data.msg)
        } else {
            proxy.$Message.error(response.data.msg)
        }
    })
}


const handleSuccess = (res, file) => {
    proxy.$Message.success(res.msg)
    proxy.$Spin.hide()
    initList(1, 15)
    upload.value.clearFiles();
}


const handleProgress = (event, file, fileList) => {
    proxy.$Spin.show()
}
</script>